package top.roadvast.springboot.compare_test.bean;

import java.io.Serializable;
import java.util.Objects;

/**
 * @Author 郭泽鹏
 * @Date 2021/10/18 14:17
 * @Version 1.0
 * @Description 自定义排序对象
 * 排序规则：先按年龄排序，后按姓名排序
 * 第一步：重写hashCode和equals方法
 * 第二步：实现Comparable，重写compare方法
 * 第三步：判断
 *      1、判断是否为同一个对象
 *      2、判断值是否相等。
 */
public class User implements Comparable<User>, Serializable {

    private Long id;

    private String username;

    private String password;

    private Integer age;

    private String email;

    private String phone;

    public User() {
    }

    public User(Long id, String username, String password, Integer age, String email, String phone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
        this.email = email;
        this.phone = phone;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id.equals(user.id) &&
                username.equals(user.username) &&
                password.equals(user.password) &&
                age.equals(user.age) &&
                email.equals(user.email) &&
                phone.equals(user.phone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, username, password, age, email, phone);
    }

    @Override
    public int compareTo(User o) {
        if (this.equals(o)) {
            return 0;
        } else if (this.age.equals(o.age)) {
            return this.username.compareTo(o.username);
        } else {
            return this.age.compareTo(o.age);
        }
    }
}
